package com.gxa.zuul.service;

import com.gxa.zuul.pojo.GuaHaoYuan;
import com.gxa.zuul.mapper.GuaHaoYuanMapper;
import com.gxa.zuul.pojo.YongHu;
import com.gxa.zuul.util.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 挂号员信息表(GuaHaoYuan)表服务实现类
 *
 * @author xyc
 * @since 2021-03-19 14:48:14
 * @version 1.0
 */
@Service
public class GuaHaoYuanServiceImpl{
    @Resource
    private GuaHaoYuanMapper guaHaoYuanMapper;
    @Resource
    private RedisTemplate redisTemplate;

    public Map<String, Object> reg(GuaHaoYuan guaHaoYuan) {
        Map<String, Object> map = new HashMap<>();
        try {
            String miMa = guaHaoYuan.getGuaHaoYuanMiMa();
            String guaHaoYuanXingMing = guaHaoYuan.getGuaHaoYuanXingMing();
            guaHaoYuan.setGuaHaoYuanMiMa(md5(guaHaoYuanXingMing, miMa));
            guaHaoYuanMapper.reg(guaHaoYuan);
            redisTemplate.opsForValue().set(guaHaoYuan.getGuaHaoYuanXingMing(), guaHaoYuan);
            map.put("code", 200);
            map.put("msg", "注册成功，请登录");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 100200);
            map.put("msg", "注册失败，请稍候重试！");
        }
        return map;
    }

    public Map<String, Object> login02(String username,String miMa,String shenFen) {
        Map<String, Object> map = new HashMap<>();

        if (redisTemplate.hasKey(username)) {
            System.out.println(redisTemplate.opsForValue().get(username));
        }
//        Object obj = redisTemplate.opsForValue().get(yongHu.getYongHuMingCheng());
//        System.out.println("obj=" + obj);
//        Map<String, Object> map = new HashMap<>();
//        if(obj != null) {
//            YongHu yongHu_redis = (YongHu)obj;
//            if(yongHu_redis.getMiMa().equals(yongHu.getMiMa())) {
//                map.put("code", 200);
//                map.put("msg", "登录成功!");
//                return map;
//            }
//        }
//        map.put("code", 10001);
//        map.put("msg", "登录失败, 用户名或者密码错误!");
        return map;
    }

    public YongHu getUser(String xingMing) {
        System.out.println(xingMing);
        Object obj = redisTemplate.opsForValue().get(xingMing);
        System.out.println(obj);
        return (YongHu)obj;
    }

    public Map<String, Object> selectAll(GuaHaoYuan guaHaoYuan) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", guaHaoYuanMapper.selectAll(guaHaoYuan));
        return map;
    }

    // 注册时，进行shiro加密，返回加密后的结果，如果在加入shiro之前，存在用户密码不是此方式加密的，那么将无法登录
    // 使用用户名作为盐值
    private String md5(String username, String password){
        String hashAlgorithmName = "MD5";                   // 加密方式
        ByteSource salt = ByteSource.Util.bytes(username);  // 以账号作为盐值
        int hashIterations = 11;                            // 加密11次
        return new SimpleHash(hashAlgorithmName, password, salt, hashIterations).toString();
    }



    public Map<String, Object> login(YongHu yongHu) {
        Map<String, Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken upToken = new UsernamePasswordToken(yongHu.getXingMing(), yongHu.getMiMa());
            upToken.setRememberMe(true);
            try {
                subject.login(upToken);    // 调用realm认证用户权限  默认会调用realm中doGetAuthenticationInfo方法
                String jwtToken = JwtUtils.createToken(yongHu.getXingMing(), yongHu.getXingMing(), yongHu.getXingMing());
                System.out.println("jwtToken=" + jwtToken);
                map.put("code", 200);
                map.put("msg", "登录成功");
                map.put("token", jwtToken);
                return map;
            } catch (IncorrectCredentialsException ice) {
                System.out.println("用户名/密码不匹配！");
                map.put("msg", "用户名/密码不匹配！");
            } catch (LockedAccountException lae) {
                System.out.println("账户已被冻结！");
                map.put("msg", "账户已被冻结！");
            } catch (UnknownAccountException uae) {
                System.out.println("账户不存在");
                map.put("msg", "账户不存在");
            } catch (AuthenticationException ae) {
                map.put("msg", "额外的错误");
                System.out.println(ae.getMessage());
            }
        }
        map.put("code", 10500);
        return map;
    }
}